package com.xebest.web.controller.logistics;

import com.xebest.common.base.controller.BASEMessage;
import com.xebest.common.base.controller.BaseController;
import com.xebest.common.base.controller.BaseResponse;
import com.xebest.constant.XeConstans;
import com.xebest.util.SysSetBeanUtils;
import com.xebest.util.XeVenExcelUtil;
import com.xebest.web.controller.export.XeVenLogisticsExport;
import com.xebest.web.form.XeVenOrderLogisticsForm;
import com.xebest.web.service.XeVenLogisticsCenterExportService;
import com.xebest.web.view.ErpUserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
 * 物流中心导出
 *
 * @author 赵盼飞
 */
@RestController
@RequestMapping("logistics")
public class XeVenLogisticsCenterExportController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(XeVenLogisticsCenterExportController.class);//日志记录类

    /**
     * 处理物流中心导出数据
     */
    @Autowired
    private XeVenLogisticsCenterExportService logisticsCenterExportService;

    @Value("${xeven.erp.exportTempletDir}")
    private String exportTempletDir;//导出文件目录

    /**
     * 物流单查询导出明细
     *
     * @author 赵盼飞
     */
    @RequestMapping("exLogisticsOrderDetail")
    public @ResponseBody
    BaseResponse exLogisticsOrderDetail(@ModelAttribute XeVenOrderLogisticsForm form, HttpServletResponse response) {
        try {
            String[] str = new String[]{
                    "venName", "createDate", "sendTime", "storeName", "storeArea", "receiptArea", "logtsCode",
                    "orderId", "proNum", "proName", "specValue", "normNum", "normWeight", "realSendNum",
                    "caConsignee", "caStreet", "carPlateNum", "contactPerson", "mobilePhone",
                    "orderRemark", "logisticsRemark"
            };
            form.setFieldArr(str);
            //1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());

            //权限使用
            if (XeConstans._XE_TWO == userInfo.getAdminFlag()) {
                if (!StringUtils.isEmpty(userInfo.getRoleIds())) {
                    form.setRoleId(userInfo.getRoleIds().split(","));
                } else {
                    form.setRoleId(null);
                }
                form.setAdmin("noAdmin");
            } else {
                form.setAdmin(null);
            }
            //2.获取数据
            List<XeVenLogisticsExport> logisticsOrderEDetail = logisticsCenterExportService.queryExLogisticsOrderDetail(form);
            if (null != logisticsOrderEDetail && logisticsOrderEDetail.size() > 0) {
                //3.导出
                boolean rs = XeVenExcelUtil.exportExcelService(logisticsOrderEDetail, new XeVenLogisticsExport(), form.getFieldArr(), exportTempletDir, "LOGISTICSDETAIL-", response);
                if (rs) {
                    return sendAjaxOK();
                } else {
                    return sendAjaxError();
                }
            } else {
                return sendAjaxOK("导出数据为空！");
            }
        } catch (Exception e) {
            log.error("物流单查询导出明细接口异常：{}", e);
            return sendAjaxError();
        }
    }


    /**
     * 物流派车导出
     *
     * @author 赵盼飞
     */
    @RequestMapping("exSendCarList")
    public @ResponseBody
    BaseResponse queryExSendCarList(@ModelAttribute XeVenOrderLogisticsForm form, HttpServletResponse response) {
        try {
            String[] str = new String[]{
                    "venName", "logtsCode", "orderId", "proName", "specValue", "normWeight", "normNum", "realSendNum",
                    "receiptArea", "carPlateNum", "contactPerson", "mobilePhone",
                    "loadAddr", "loadTime", "appointCar", "logisticsRemark", "orderRemark", "printStatus"
            };
            form.setFieldArr(str);
            //1、验证参数
            if (form.getFieldArr().length <= 0) {
                return sendAjaxError(BASEMessage.PARAM_ERROR);
            }
            ErpUserInfo userInfo = SysSetBeanUtils.getErpUserInfo(super.getErpLoginUserInfo());
            form.setUserCode(userInfo.getUserCode());
            //权限使用
            if (XeConstans._XE_TWO == userInfo.getAdminFlag()) {
                if (!StringUtils.isEmpty(userInfo.getRoleIds())) {
                    form.setRoleId(userInfo.getRoleIds().split(","));
                } else {
                    form.setRoleId(null);
                }
                form.setAdmin("noAdmin");
            } else {
                form.setAdmin(null);
            }
            //2.获取数据
            List<XeVenLogisticsExport> logisticsOrderEDetail = logisticsCenterExportService.queryExSendCarList(form);
            if (null != logisticsOrderEDetail && logisticsOrderEDetail.size() > 0) {
                //3.导出
                boolean rs = XeVenExcelUtil.exportExcelService(logisticsOrderEDetail, new XeVenLogisticsExport(), form.getFieldArr(), exportTempletDir, "SENDCARLIST-", response);
                if (rs) {
                    return sendAjaxOK();
                } else {
                    return sendAjaxError();
                }
            } else {
                return sendAjaxOK("导出数据为空！");
            }
        } catch (Exception e) {
            log.error("物流派车导出接口异常：{}", e);
            return sendAjaxError();
        }
    }
}
  
